package cn.neusoft.controller;


import cn.neusoft.pojo.Employee;
import cn.neusoft.pojo.Role;
import cn.neusoft.pojo.Scheduling;
import cn.neusoft.query.EmployeeQuery;
import cn.neusoft.service.IEmployeeService;
import cn.neusoft.service.IRoleService;
import cn.neusoft.service.ISchedulingService;
import cn.neusoft.util.MD5Utils;
import cn.neusoft.vo.ResultBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hxj
 * @since 2024-12-18
 */
@Api(description = "员工管理")
@RestController
@RequestMapping("/employee")
public class EmployeeController extends BaseController<Employee, EmployeeQuery>{

    @Autowired
    ISchedulingService schedulingService;

    @Autowired
    IEmployeeService employeeService;
    
    @Autowired
    IRoleService roleService;

    @ApiOperation("根据条件查询-不分页不联查")
    @PostMapping("/conditions")
    public ResultBean getByConditions(@RequestBody EmployeeQuery employeeQuery){
        System.out.println(employeeQuery.getScheduleIndex()+"  "+employeeQuery.getDeptmentId()+"  "+employeeQuery.getRegistLevelId()+"  "+employeeQuery.getRoleName());

        //查到所有的排班信息
        List<Scheduling> schedulingList = schedulingService.list();
        //遍历排班信息
        schedulingList.forEach(scheduling -> {
            System.out.println(scheduling.getWeekRule());
            //传递的看诊时间对应的下标处 是不是1  如果是 记录这个排班的id值
            char c = scheduling.getWeekRule().charAt(employeeQuery.getScheduleIndex());
            if (c=='1'){
                employeeQuery.setScheduleId(scheduling.getId());
            }
        });

        //根据传递的角色名称 查询角色id
        Role role = roleService.getOne(new QueryWrapper<Role>().eq("name", employeeQuery.getRoleName()));
        employeeQuery.setRoleId(role.getId());

        //查询对应的医生
        List<Employee> list = employeeService.list(employeeQuery.getConditionWrapper());
        return ResultBean.SUCCESS(list);
    }

    @ApiOperation("重置密码")
    @PutMapping("/resetpass/{id}")
    public ResultBean resetPassword(@PathVariable("id") Integer employeeId){
        UpdateWrapper<Employee> updateWrapper = new UpdateWrapper<>();
        //密码加密  工具类的方法  传入明文返回密文
        String encrypt = MD5Utils.encrypt("111111");
        updateWrapper.set("password",encrypt);
        updateWrapper.eq("id",employeeId);

        boolean update = employeeService.update(updateWrapper);
        return update?ResultBean.SUCCESS():ResultBean.ERROR("重置密码失败");
    }

}
